package com.seed.util;

import cn.hutool.core.util.StrUtil;
import com.seed.core.action.Action;
import com.seed.core.column.Column;
import com.seed.core.enumeration.SqlOp;
import com.seed.core.listener.OnSetListener;
import com.seed.core.pojo.seed.*;
import com.seed.core.query.pojo.Declared;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;

public class SeedKit {
    public static String getLabel(Seed seed) {
        String label = seed.getLabel();
        if(StrUtil.isBlank(label)) throw new RuntimeException(String.format("请在【%s】下配置label的值", seed.getName()));

        return label;
    }

    public static Map<String, Action> getActions(Seed seed) {
        return seed.getActions();
    }

    public static Action getAction(Seed seed, RequestMethod method, String actionName) {
        if(seed == null || StrUtil.isBlank(actionName)) return null;

        Map<String, Action> actions = seed.getActions();
        if(actions == null) return null;

        Action action = actions.get(actionName);
        if(action == null || action.getMethods() == null) return null;

        return action.getMethods().contains(method) ? action : null;
    }

//    public static List<String> getDeclares(Seed seed, RequestMethod method, String actionName) {
//        Action action = getAction(seed, method, actionName);
//        if(action == null) {
//            throw new RuntimeException(String.format("【%s】下【%s】不存在", seed.getName(), actionName));
//        }
//
//        return getDeclares(seed, action);
//    }

    public static List<String> getDefaultDeclares(Seed seed) {
        return seed.getColumns().values()
                .stream()
                .filter(e -> !e.isLarge())
                .map(Column::getName)
                .toList();
    }

//    public static List<String> getSearches(Seed seed, RequestMethod method, String actionName) {
//        Action action = getAction(seed, method, actionName);
//        if(action == null) {
//            throw new RuntimeException(String.format("【%s】下【%s】不存在", seed.getName(), actionName));
//        }
//
//        return getSearches(seed, action);
//    }

//    public static List<String> getOrders(Seed seed, RequestMethod method, String actionName) {
//        Action action = getAction(seed, method, actionName);
//        if(action == null) {
//            throw new RuntimeException(String.format("【%s】下【%s】不存在", seed.getName(), actionName));
//        }
//
//        return getOrders(seed, action);
//    }

//    public static Map<String, Stat> getStats(Seed seed, RequestMethod method, String actionName) {
//        Action action = getAction(seed, method, actionName);
//        if(action == null) {
//            throw new RuntimeException(String.format("【%s】下【%s】不存在", seed.getName(), actionName));
//        }
//
//        return getStats(seed, action);
//    }

    public static Map<String, Relation> getRelations(Seed seed) {
        return seed.getRelations();
    }

    public static Relation getRelation(Seed seed, String cross) {
        return getRelations(seed).get(cross);
    }

    public static List<String> getColumnNames(Seed seed) {
        return new ArrayList<>(seed.getColumns().keySet());
    }


    public static Column getNameColumn(Seed seed) {
        String nameColumn = seed.getNameColumn();
        if(StrUtil.isNotBlank(nameColumn)) {
            String columnName = StrUtil.toUnderlineCase(seed.getNameColumn());
            Column column = seed.getColumns().get(columnName);
            if(column == null) {
                throw new RuntimeException(String.format("【%s】下列【%s】不存在", seed.getName(), nameColumn));
            }
            return column;
        }

        Column column = seed.getColumns().get("name");
        if(column == null) ThrowKit.exception("请为【%s】配置【nameColumn】", seed.getName());

        return column;
    }

    public static List<? extends OnSetListener> getOnSet(Seed seed) {
        return seed.getOnSet();
    }

    public static SqlOp defaultOp(Seed seed, String columnName) {
        Column column = seed.getColumns().get(StrUtil.toUnderlineCase(columnName));
        if(column == null) {
            throw new RuntimeException(String.format("【%s】下不存在列【%s】", seed.getLabel(), columnName));
        }

//        return switch (column.getType()) {
//            case CHAR, TINYINT, SMALLINT, MEDIUMINT, INT, DOUBLE, FLOAT, DECIMAL -> SqlOp.eq;
//            default -> SqlOp.like;
//        };
        return column.getDefaultOp();
    }

    public static Declared getDeclared(Seed seed, String declare) {
        Map<String, Column> columns = seed.getColumns();
        if(declare.contains(".") || "".equals(declare) || "*".equals(declare)) throw new RuntimeException("格式错误");

        Column column = columns.get(declare);
        if(column != null) return new Declared(column);

        Relation relation = getRelation(seed, declare);
        if(relation == null) throw new RuntimeException(String.format("【%s】既不是列也不是关系", declare));

        return new Declared(relation);
    }

    /**
     * 格式化扩展条件
     * @param extraSeed: 扩展模块
     * @param extra: 扩展条件参数
     * @return 格式化后列名变成下划线式，并且确保列存在
     */
    public static Map<String, Object> formatExtra(Seed extraSeed, Map<String, Object> extra) {
        Map<String, Object> ret = new HashMap<>();
        if(extra == null) return ret;

        for(Map.Entry<String, Object> entry: extra.entrySet()) {
            String columnName = StrUtil.toUnderlineCase(entry.getKey());
            if (!extraSeed.getColumns().containsKey(columnName)) {
                ThrowKit.exception("【%s】中不存在列【%s】", extraSeed.getName(), columnName);
            }
            ret.put(columnName, entry.getValue());
        }
        return ret;
    }
}
